<?php
require_once FOREGROUND_COMMON_ACTION_SERVICE_DIR . '/ajax.php';

class foreground_member_address_update_action_service extends foreground_ajax_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {	     		     
	    //var_dump(__METHOD__); exit();
        //var_dump($_SESSION); exit();
    
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        //var_dump($_POST); exit();
        $address_id = init_user_data_key($_POST, 'address_id', 'int');        

	    $receive_province_id = init_user_data_key($_POST, 'province_id', 'str');
	    $receive_city_id = init_user_data_key($_POST, 'city_id', 'str');
	    $receive_district_id = init_user_data_key($_POST, 'district_id', 'str');
	    $receive_street_id = init_user_data_key($_POST, 'street_id', 'str', '0');
                
        $receive_address = init_user_data_key($_POST, 'receive_address', 'str');
        $receiver = init_user_data_key($_POST, 'receiver', 'str');
        $receive_mobile = init_user_data_key($_POST, 'receive_mobile', 'str');
        $receive_tel = init_user_data_key($_POST, 'receive_tel', 'str');
    
        $action_data = wwy_action::$data;
	    $session_member_info = $action_data['session_member_info'];
        $member_id = $session_member_info['id'];
            
        if(empty($address_id)){
            $result['status']  = -400;
            $result['message'] = '修改失败';
        }else{        
            $model_foreground_address_region = wwy_resource_container::get_model_foreground_address_region();
            //var_dump($model_foreground_address_region); exit();
            $model_foreground_address_region->check_region($receive_province_id, $receive_city_id, $receive_district_id, $receive_street_id);
            //exit();
            
            if(empty($receive_address)){
                $result['status']  = -2;
                $result['message'] = '请输入收货地址';
            }else if(empty($receiver)){
                $result['status']  = -3;
                $result['message'] = '请输入收货人';
            }else if(empty($receive_mobile) && empty($receive_tel)){
                $result['status']  = -4;
                $result['message'] = '请输入手机或电话';
            }else{
                $model_foreground_member_address = wwy_resource_container::get_model_foreground_member_address();
                //var_dump($model_foreground_member_address); exit();            
                $where      = array(
                    'id'        => $address_id,
                    'member_id' => $member_id,
                );
                $address_row = $model_foreground_member_address->fetch_row($where);
                //var_dump($address_row); exit();
                
                if(empty($address_row)){
                    $result['status']  = -400;
                    $result['message'] = '修改失败';
                }else{    
                    $member_address_info = array(
                        'id'          => $address_id,
                        'province_id' => $receive_province_id,
                        'city_id'     => $receive_city_id,
                        'district_id' => $receive_district_id,
                        'street_id'   => $receive_street_id,
                        
                        'address'     => htmlspecialchars($receive_address),
                        'receiver'    => htmlspecialchars($receiver),
                        'mobile'      => htmlspecialchars($receive_mobile),
                        'tel'         => htmlspecialchars($receive_tel)
                    );
                    $update_result = $model_foreground_member_address->update($member_address_info);
                    //var_dump($update_result); exit();
            
                    if(!$update_result){
                        $result['status']  = -4;
                        $result['message'] = '修改失败';
                    }else{
                        $result['status']  = 1;
                        $result['message'] = '修改成功';
                    }
                }
            } 
        }
	
	    self::set_view_status($result['status']);
	    self::set_view_message($result['message']);
    }
}